package com.hhkj.admin.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.hhkj.admin.course.model.req.CourseReq;
import com.hhkj.admin.student.model.req.StudentReq;
import com.hhkj.admin.student.model.res.StudentExclRes;
import com.hhkj.admin.student.model.res.StudentRes;
import com.hhkj.admin.student.service.biz.StudentBizService;
import com.ruoyi.common.annotation.LogTitlePrefix;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.BaseController;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import com.ruoyi.common.valid.VEdit;
import com.ruoyi.system.domain.dto.StudentDto;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hhkj.admin.student.domain.Student;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学员信息Controller
 *
 * @author ruoyi
 * @since 2023-05-12
 */
@LogTitlePrefix("学员信息")
@PermissionPrefix("student:student")
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController<Student, StudentReq, StudentRes, StudentBizService> {
    @Autowired
    private StudentBizService studentBizService;

    /**
     * 下载表格
     *
     * @param response
     */
    @PreAuthorize("@ss.hasPermi(this,'downexcl')")
    @PostMapping("/download/excl")
    public void downloadExcl(HttpServletResponse response) {
        Workbook template = studentBizService.getStudentTemp();
        ExcelUtils.downLoadExcel("学员导入模板", response, template);
    }

    /**
     * 导入表格
     *
     * @param file
     */
    @PreAuthorize("@ss.hasPermi(this,'import')")
    @PostMapping("/import/excl")
    @Transactional(rollbackFor = Exception.class)
        public AjaxResult importExcl(@RequestParam(value = "file") MultipartFile file) {
        return AjaxResult.success(studentBizService.importExcl(file));
    }


    /**
     * 学员批量指定企业
     */
    @PreAuthorize("@ss.hasPermi(this,'setEnt')")
    @Log(title = "学员批量指定企业", businessType = BusinessType.UPDATE)
    @PostMapping("/batch/setEnt")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult batchSetEnt(@Validated(VEdit.class) @RequestBody StudentDto studentVo) {
        return studentBizService.batchSetEnt(studentVo) ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 生成科目二维码
     */
    @Log(title = "生成科目二维码", businessType = BusinessType.OTHER)
    @PostMapping("/create/qr")
    public void createQr(@RequestBody CourseReq course, HttpServletResponse response) {
        studentBizService.createCourseQr(course, response);
    }

    /**
     * 分页查询学员信息
     */
    @Override
    public TableDataInfo page(@RequestBody StudentReq studentReq) {
        startPageBody(studentReq);
        return getDataTable(studentBizService.selectStudentList(studentReq));
    }


    /**
     * 获取学员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi(this,'query')")
    @PostMapping(value = "/detail")
    public AjaxResult getInfo(@RequestBody StudentReq studentReq) {
        return success(studentBizService.selectStudentById(studentReq));
    }

    /**
     * 批量删除学员信息
     */
    @PreAuthorize("@ss.hasPermi(this,'remove')")
    @Log(title = "学员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/batch/del")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@RequestBody StudentDto studentVo) {
        if (studentVo.getIdList().size() == 0) {
            throw new GlobalException("未选择学员！");
        }
        return toAjax(studentBizService.removeStudentByIdList(studentVo.getIdList()));
    }
}
